package com.tuo.lf.smartfloatinghelper;


import androidx.annotation.Nullable;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.media.projection.MediaProjectionManager;
import static android.app.Activity.RESULT_OK;

public class MainActivity extends AppCompatActivity {
    private static final int SCREEN_RECORD_REQUEST_CODE = 100;
    private MediaProjectionManager mediaProjectionManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        // 添加开启和关闭按钮
        Button startButton = findViewById(R.id.start_floating_window);
        Button stopButton = findViewById(R.id.stop_floating_window);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(MainActivity.this)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, 1);
                } else {
                    // 请求屏幕录制权限
                    Intent captureIntent = mediaProjectionManager.createScreenCaptureIntent();
                    startActivityForResult(captureIntent, SCREEN_RECORD_REQUEST_CODE);
                }
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(new Intent(MainActivity.this, FloatingWindowService.class));
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SCREEN_RECORD_REQUEST_CODE && data != null) {
            Intent serviceIntent = new Intent(this, FloatingWindowService.class);
            serviceIntent.putExtra("resultCode", resultCode);
            serviceIntent.putExtra("resultData", data);
            startService(serviceIntent);
        }
        // if (requestCode == 1) {
        //     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.canDrawOverlays(this)) {
        //         // 先不启动服务，等待屏幕录制权限结果
        //     } else {
        //         Toast.makeText(this, "未授予悬浮窗权限", Toast.LENGTH_SHORT).show();
        //     }
        // } else if (requestCode == SCREEN_RECORD_REQUEST_CODE) {
        //     if (resultCode == RESULT_OK) {
        //         Intent serviceIntent = new Intent(this, FloatingWindowService.class);
        //         serviceIntent.putExtra("resultCode", resultCode);
        //         serviceIntent.putExtra("resultData", data);
        //         startService(serviceIntent);
        //     } else {
        //         Toast.makeText(this, "未授予屏幕录制权限", Toast.LENGTH_SHORT).show();
        //     }
        // }
    }
}
